package com.lxh.lxhcrm.controller;

import com.alibaba.fastjson.JSONObject;
import com.lxh.lxhcrm.model.pojos.Emp;
import com.lxh.lxhcrm.model.pojos.Function;
import com.lxh.lxhcrm.model.pojos.FunctionType;
import com.lxh.lxhcrm.model.service.FunTyService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class FunTyController {
    @Autowired
    FunTyService funTyService;
    /*根据权限组id查询 所有的权限*/
    @RequestMapping("/fun-byId")
    @ResponseBody
    public List<Function> findFunByFunTyId(String funTypeName){
        return funTyService.findFunByFunTyId(funTypeName);
    }
    /*根据职位name 和部门id 查询 该权限组已被选的权限id的list*/
    @RequestMapping("/fun-byPostId")
    @ResponseBody /*@RequestParam("str") String str*/
    public List<Integer> findFunByPostId(String funTypeName,Integer positionId){
        /*获取职位id*//*
        JSONObject parse = (JSONObject)JSONObject.parse(str);
        System.out.println(parse);
        String funTypeName=(String) parse.get("funTypeName");
        //先转成JSON对象
        JSONObject jsonObject = (JSONObject)parse.get("emp");
        //JSON对象转换成Java对象
        Emp emp = JSONObject.toJavaObject(jsonObject, Emp.class);*/
        System.out.println("funTypeName"+funTypeName);
        System.out.println(positionId+"postId");
        return funTyService.findFunByPostId(positionId,funTypeName);
    }

/*查询职位所有已有权限 左边导航栏*/
    @RequestMapping("/fun-menu")
    @ResponseBody
    public List<FunctionType> funMenu(Integer empId){
        /*从前台传过来的部门id*/

        System.out.println("从前台传过来的empId"+empId);
        Integer  positionId =funTyService.findPoId(empId);
        System.out.println(funTyService.funMenu(empId,positionId)+"查询职位所有已有权限 左边导航栏");

        return funTyService.funMenu(empId,positionId);
    }
    /*根据前台传过来的empId 查询是否有功能显示 返回字符串数组*/
    @RequestMapping("/check-FunTy")
    @ResponseBody
    public String checkFunTy(Integer empId){
        System.out.println(funTyService.checkFunTy(empId)+"根据前台传过来的empId 查询是否有功能显示");
        String str = funTyService.checkFunTy(empId).toString();
        return str;
    }

    /*保存权限*/
    @RequestMapping("/save-quan")
    @ResponseBody
    public void saveQuan(@RequestParam("str") String str){
        JSONObject parse = (JSONObject)JSONObject.parse(str);
        Integer positionId = (Integer)parse.get("positionId");
        List<Integer> funId = (List<Integer>)parse.get("funId");
        String funTypeName=(String) parse.get("funTypeName");
        System.out.println("保存过来的职位id"+positionId);
        System.out.println("保存过来的权限id组"+funId);
        System.out.println("funTypeName"+funTypeName);
        funTyService.saveQuan(positionId,funTypeName,funId);
    }

}
